<?php
namespace App\Service;
use App\Exceptions\ApiRequestException;
use Illuminate\Support\Facades\Cache;
use PHPMailer\PHPMailer\PHPMailer;
# 邮箱
class EmailService
{
    protected $seconds = 15*60;# 15分钟
    public function sendMail($title, $content, array $sendMailer = [])
    {
        $mail = new PHPMailer(true);
        $config = config('email');
        try {
            //Server settings
            $mail->SMTPDebug = 0;                                       // Enable verbose debug output
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host = $config['SMTP_SERVER']; //  'smtpdm.aliyun.com'; //  $config['SMTP_SERVER'];//'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                                   // Enable SMTP authentication
            $mail->Username = $config['SMTP_SEND_EMAIL'];  // 'servers@cdsile.com';//   $config['SMTP_SEND_EMAIL']; // 'user@example.com';                     // SMTP username
            $mail->Password = $config['SMTP_SEND_PASSWORD']; //  'CDsile2017';  //$config['SMTP_SEND_PASSWORD'];//'secret';                               // SMTP password
            $mail->SMTPSecure = 'ssl';//  'tls';                                  // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465; //25; //$config['SMTP_PORT']; //587;                                    // TCP port to connect to
            $mail->CharSet = 'UTF-8';
            //Recipients
            $mail->setFrom($config['SMTP_SEND_EMAIL'], $config['SMTP_SEND_NICKNAME']);
            if (!empty($sendMailer)) {
                foreach ($sendMailer as $key => $val) {
                    $mail->addAddress($val);     // Add a recipient
                }
            }

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $title;
            $mail->Body = $content;
            $mail->send();
//            echo 'Message has been sent';
        } catch (Exception $e) {
            $message =  "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            throw new \App\Exceptions\ApiRequestException($message);
        }
        return 'success';
    }

    public function sendCode($email,$type='forgetpassword'){
        $code = makeCode(4);
        $key = $this->getKey($type, $email);
//        dd($code,$key);
        $this->sendMail('找回秘密',"你的验证码是 {$code} 15分钟内有效",[$email]);
        Cache::put($key, $code, $this->seconds);
    }
    public function getKey($type,$phone)
    {
        $phone = trim($phone);
        return $type.'_'.$phone;
    }

    public function validate($email,$code, $type='forgetpassword')
    {
        $key = $this->getKey($type,$email);
        $cache_code = Cache::get($key);
//        dd($cache_code,$key);
        if ($code != $cache_code) {
            throw  new ApiRequestException('验证码不对');
        }

    }

    public function delete($type,$email)
    {
        $key = $this->getKey($type,$email);
        Cache::forget($key);
    }

}
